package city.spring.modules.consumptionItem.controller;

import city.spring.modules.bed.service.BedManagementService;
import city.spring.modules.bed.vo.BedInfoVO;
import city.spring.modules.consumptionItem.entity.ProjectBedRelationEntity;
import city.spring.modules.consumptionItem.repository.ProjectBedRelationRepository;
import city.spring.modules.consumptionItem.service.ProjectBedRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Controller：项目-床位联系表
 *
 * @author pxm
 * @date 2020-08-18 09:02:27
 */
@RestController
@RequestMapping("/project_bed_relation/{projectId}/bed")
public class ProjectBedRelationController extends ApiController {
    private final ProjectBedRelationService projectBedRelationService;
    private final ProjectBedRelationRepository projectBedRelationRepository;
    private final BedManagementService bedManagementService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<ProjectBedRelationEntity, ?>> entityOrderFields;

    public ProjectBedRelationController(ProjectBedRelationService projectBedRelationService, ProjectBedRelationRepository projectBedRelationRepository, BedManagementService bedManagementService) {
        this.projectBedRelationService = projectBedRelationService;
        this.projectBedRelationRepository = projectBedRelationRepository;
        this.bedManagementService = bedManagementService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(ProjectBedRelationEntity::getId);
        entityOrderFields.add(ProjectBedRelationEntity::getProjectId);
        entityOrderFields.add(ProjectBedRelationEntity::getBuildId);
        entityOrderFields.add(ProjectBedRelationEntity::getFloorId);
        entityOrderFields.add(ProjectBedRelationEntity::getRoomId);
        entityOrderFields.add(ProjectBedRelationEntity::getBedId);
    }

    @ModelAttribute
    public void getProjectId(@PathVariable String projectId) {
        if (projectId == null) {
            throw new RuntimeException("项目ID必须不为空");
        }
    }

    /**
     * 给消费项目添加多个床位信息
     *
     * @param projectId 消费项目
     * @param bedIds    所添加的床位信息ID列表
     * @return
     */
    @PostMapping
    public Object addAll(@PathVariable String projectId, @RequestBody List<String> bedIds) {
        // 删除该项目下所有的床位关联数据
        projectBedRelationService.lambdaUpdate().eq(ProjectBedRelationEntity::getProjectId, projectId).remove();
        List<BedInfoVO> bedInfos = bedManagementService.getBedInfoByBedIds(bedIds);
        List<ProjectBedRelationEntity> saveList = bedInfos.stream()
                .map(vo -> {
                    ProjectBedRelationEntity entity = new ProjectBedRelationEntity();
                    entity.setProjectId(projectId);
                    entity.setBuildId(vo.getBuilding().getId());
                    entity.setFloorId(vo.getFloor().getId());
                    entity.setRoomId(vo.getRoom().getId());
                    entity.setBedId(vo.getBed().getId());
                    return entity;
                }).collect(Collectors.toList());
        // 重新保存所有关联的床位数据
        if (!saveList.isEmpty()) {
            projectBedRelationService.saveBatch(saveList);
        }

        return success(null);
    }

    /**
     * 获取消费项目的多个床位信息列表
     *
     * @param projectId 活动ID
     */
    @GetMapping
    public Object list(@PathVariable String projectId) {
        return success(projectBedRelationRepository.getProjectBeds(projectId));
    }


//    /**
//     * 获取全部的 <strong>项目-床位联系表</strong> 列表
//     */
//    @GetMapping("all")
//    public Object listAll(@PageableDefault Pageable pageable, ProjectBedRelationEntity entity) {
//        LambdaQueryChainWrapper<ProjectBedRelationEntity> lambdaQuery = buildLambdaQuery(entity);
//        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
//        return success(lambdaQuery.list());
//    }
//
//    /**
//     * 分页获取 <strong>项目-床位联系表</strong> 列表
//     *
//     * @param pageable 分页参数信息
//     */
//    @GetMapping
//    public Object list(@PageableDefault Pageable pageable, ProjectBedRelationEntity entity) {
//        LambdaQueryChainWrapper<ProjectBedRelationEntity> lambdaQuery = buildLambdaQuery(entity);
//        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
//        Page<ProjectBedRelationEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
//        return success(page);
//    }

//    /**
//     * 获取一个 <strong>项目-床位联系表</strong>
//     *
//     * @param id 主键ID
//     */
//    @GetMapping("{id}")
//    public Object info(@PathVariable String id) {
//        return success(projectBedRelationService.getById(id));
//    }

//    /**
//     * 添加一个 <strong>项目-床位联系表</strong>
//     *
//     * @param entity 修改后的信息
//     */
//    @PostMapping
//    public Object add(@RequestBody ProjectBedRelationEntity entity) {
//        projectBedRelationService.saveProjectBedRelation(entity);
//
//        HttpHeaders headers = new HttpHeaders();
//        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
//        return new ResponseEntity<>(headers, HttpStatus.CREATED);
//    }

//    /**
//     * 修改一个 <strong>项目-床位联系表</strong>
//     *
//     * @param id     主键ID
//     * @param entity 修改后的信息
//     */
//    @PutMapping("{id}")
//    public Object update(@PathVariable String id, @RequestBody ProjectBedRelationEntity entity) {
//        entity.setId(id);
//        projectBedRelationService.updateProjectBedRelation(entity);
//        return new ResponseEntity<>(HttpStatus.OK);
//    }
//
//    /**
//     * 删除一个 <strong>项目-床位联系表</strong>
//     *
//     * @param id 主键ID
//     */
//    @DeleteMapping("{id}")
//    public Object delete(@PathVariable String id) {
//        projectBedRelationService.deleteProjectBedRelation(id);
//        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
//    }
//
//    /**
//     * 删除多个 <strong>项目-床位联系表</strong>
//     *
//     * @param ids 主键ID列表
//     */
//    @DeleteMapping
//    public Object deleteIds(@RequestBody List<String> ids) {
//        projectBedRelationService.deleteProjectBedRelation(ids);
//        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
//    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<ProjectBedRelationEntity> buildLambdaQuery(ProjectBedRelationEntity entity) {
        LambdaQueryChainWrapper<ProjectBedRelationEntity> lambdaQuery = projectBedRelationService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), ProjectBedRelationEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getProjectId()), ProjectBedRelationEntity::getProjectId, entity.getProjectId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getBuildId()), ProjectBedRelationEntity::getBuildId, entity.getBuildId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getFloorId()), ProjectBedRelationEntity::getFloorId, entity.getFloorId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRoomId()), ProjectBedRelationEntity::getRoomId, entity.getRoomId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getBedId()), ProjectBedRelationEntity::getBedId, entity.getBedId());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<ProjectBedRelationEntity> buildQuery(ProjectBedRelationEntity entity) {
        QueryWrapper<ProjectBedRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getProjectId()), "project_id", entity.getProjectId());
        wrapper.like(StringUtils.isNotBlank(entity.getBuildId()), "build_id", entity.getBuildId());
        wrapper.like(StringUtils.isNotBlank(entity.getFloorId()), "floor_id", entity.getFloorId());
        wrapper.like(StringUtils.isNotBlank(entity.getRoomId()), "room_id", entity.getRoomId());
        wrapper.like(StringUtils.isNotBlank(entity.getBedId()), "bed_id", entity.getBedId());
        return wrapper;
    }

}
